package com.san.app.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.san.app.domain.WarningDTO;
import com.san.app.service.WarningReviewService;
import com.san.app.service.WarningSubService;
import com.san.app.task.WarningSubThread;
import com.san.app.vo.WarningSubDTO;
import com.san.app.vo.WarningSubVO;
import com.san.base.controller.BaseController;
import com.san.common.annotation.Log;
import com.san.common.utils.PageUtils;
import com.san.common.utils.Query;
import com.san.common.utils.R;
import com.san.oms.domain.QuotaDO;
import com.san.oms.service.QuotaService;

/**
 * 信用预警控制层
 * @author SuperSR
 * @date 2019年3月18日 下午2:28:15
 *
 */
@RequestMapping("/app/warningsub")
@Controller
public class WarningSubController extends BaseController{
	
	@Autowired
	private WarningSubService warningSubService;
	@Autowired
	private QuotaService quotaService;
	@Autowired
	private WarningReviewService warningReviewService;
	
	private static final Logger logger = LoggerFactory.getLogger(WarningSubController.class);
	
	private String PREFIX = "app/warningsub";
	
	@GetMapping("")
	@RequiresPermissions("app:warningsub:view")
	String source(Model model) {
		return PREFIX + "/view";
	}

	/**
	 * 进入预定模板查询列表
	 * @param params
	 * @return
	 */
	@GetMapping("/list")
	@RequiresPermissions("app:warningsub:view")
	@ResponseBody
	public PageUtils list(@RequestParam Map<String, Object> params) {
		PageUtils pageUtils = null;
		List<WarningDTO> list = null;
		try {
			Long operator = getUserId();
			Query query = new Query(params);
			query.put("operator", operator);
			list = warningSubService.subscribeList(query);
			int total = warningSubService.count(query);
			pageUtils = new PageUtils(list, total);
		} catch (Exception e) {
			logger.error("获取订阅信息数据出错",e);
		}
		return pageUtils;
	}
	
	/**
	 * 预警订阅
	 * @param modelId
	 * @return
	 */
	@PostMapping(value = "/subscribe")
	@RequiresPermissions("app:warningsub:subscribe")
	@Log("信用预警订阅")
	@ResponseBody
	public R subscribe(Long modelId) {
		try {
			WarningSubVO warningSubVO = new WarningSubVO();
			warningSubVO.setModelId(modelId);
			warningSubVO.setDeptId(getDeptId());
			warningSubVO.setOperator(getUserId());
			warningSubVO.setState(0);
			warningSubService.subscribe(warningSubVO);
			return R.ok("预警模板订阅成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("预警模板订阅失败");
		}
		return R.ok("预警模板订阅失败");
	}
	/**
	 * 取消预警订阅
	 * @param id
	 * @return
	 */
	@PostMapping(value = "/cancelSubscribe")
	@RequiresPermissions("app:warningsub:subscribe")
	@Log("取消信用预警订阅")
	@ResponseBody
	public R cancelSubscribe(Long modelId) {
		try {
			WarningSubVO warningSubVO = new WarningSubVO();
			warningSubVO.setModelId(modelId);
			warningSubVO.setOperator(getUserId());
			warningSubVO.setDeptId(getDeptId());
			warningSubService.cancelSubscribe(warningSubVO);
			return R.ok("预警模板取消订阅成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("预警模板取消订阅失败");
		}
		return R.ok("预警模板取消订阅失败");
	}
	
	/**
	 * 模型指标列表页
	 * 
	 * @param model
	 * @return
	 */
	@GetMapping("quotaview/{modelId}")
	@RequiresPermissions("app:warningsub:quotaview")
	String view(Model model, @PathVariable("modelId") Long modelId) {
		model.addAttribute("modelId", modelId);
		return PREFIX + "/quotaview";
	}

	/**
	 * 获取指标模型数据
	 * 
	 * @return
	 */
	@GetMapping("quotaList")
	@RequiresPermissions("app:warningsub:quotaview")
	@ResponseBody
	List<QuotaDO> quotaList(@RequestParam Map<String, Object> params) {
		List<QuotaDO> list = null;
		try {
			String modelId = (String)params.get("modelId");
			if (null == modelId || "".equals(modelId)) {
				return new ArrayList<QuotaDO>();
			}
			
			Long mid = Long.parseLong(modelId);
			
			list = quotaService.getQuotaList(mid);
		} catch (Exception e) {
			list = new ArrayList<QuotaDO>();
			logger.error("获取指标列表出错",e);
		}

		return list;
	}
	
	/**
	 * 历史订阅数据页
	 * @param model
	 * @return
	 */
	@GetMapping("subHistory/{modelId}")
	@RequiresPermissions("app:warningsub:historyview")
	String subHistory(Model model, @PathVariable("modelId") Long modelId) {
		model.addAttribute("modelId", modelId);
		return PREFIX + "/historyview";
	}
	
	/**
	 * 加载历史订阅信息
	 * @param params
	 * @return
	 */
	@GetMapping("/history")
	@RequiresPermissions("app:warningsub:historyview")
	@ResponseBody
	public PageUtils history(@RequestParam Map<String, Object> params) {
		PageUtils pageUtils = null;
		List<WarningDTO> list = null;
		try {
			//Query query = new Query(params);
			params.put("operator", getUserId());
			Query query = new Query(params);
			list = warningReviewService.reviewList(query);
			int total = warningReviewService.count(query);
			pageUtils = new PageUtils(list, total);
		} catch (Exception e) {
			logger.error("获取历史审核信息数据出错",e);
		}
		return pageUtils;
	}
	
	/**
	 * 数据匹配
	 * @param model
	 * @return
	 */
	@GetMapping("dataMatch/{modelId}")
	@RequiresPermissions("app:warningsub:dataMatch")
	String dataMatch(Model model, @PathVariable("modelId") Long modelId) {
		Date date = new Date();
		model.addAttribute("batchNo", "9" + date.getTime());
		model.addAttribute("modelId", modelId);
		return PREFIX + "/dataMatch";
	}
	
	/**
	 * 导入
	 */
	@RequiresPermissions("app:warningsub:dataMatch")
	@Log("执行信用预警数据匹配")
	@PostMapping("/doMatch")
	@ResponseBody
	public R doMatch(WarningSubDTO warningSubDTO) {
		try {
			warningSubDTO.setUserId(getUserId());
			WarningSubThread thread = new WarningSubThread(warningSubService, warningSubDTO);
			thread.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return R.ok("数据导入已转入后台处理，稍后请在我的通知中查看导入结果，导入批次号为：" + warningSubDTO.getBatchNo());
	}
}
